<%@ include file="Common.jsp" %>
<%@ page import="de.localsport.util.MatchResultUpdator" %>

<%!
//
//   Filename: MatchResult.jsp
//   Generated with CodeCharge  v.1.2.0
//   JSP.ccp build 05/21/01
//

static final String sFileName = "BetResult.jsp";
              
%>
<%
String cSec = checkSecurity(1, session, response, request);
if ("sendRedirect".equals(cSec) ) return;
                
boolean bDebug = false;

String sAction = getParam( request, "FormAction");
String sForm = getParam( request, "FormName");
String sMemberId = (String) session.getAttribute("UserID");
String sMatchesErr = "";

java.sql.Connection conn = null;
java.sql.Statement stat = null;

String sErr = loadDriver();
conn = cn();
stat = conn.createStatement();
if ( ! sErr.equals("") ) {
 try {
   out.println(sErr);
 }
 catch (Exception e) {}
}
if ( sForm.equals("Matches") ) {
  sMatchesErr = MatchesAction(request, response, session, out, sAction, sForm, conn, stat);
  if ( "sendRedirect".equals(sMatchesErr)) return;
}

%>            
<html>
<head>
<title>Soccer Zocker - Place Bet</title>

</head>
<body style="background-color: #006600; color: #000000; font-family: Arial, Tahoma, Verdana, Helveticabackground-color: #FFFFFF; color: #000000; font-family: Arial, Tahoma, Verdana, Helvetica">
<jsp:include page="Header.jsp" flush="true"/>
 <table>
  <tr>   
   <td valign="top"><center>
<% Matches_Show(request, response, session, out, sMatchesErr, sForm, sAction, conn, stat); %>
    <!--<SCRIPT Language="JavaScript">
if (document.forms["Matches"])
document.Matches.onsubmit=delconf;
function delconf() {
if (document.Matches.FormAction.value == 'delete')
  return confirm('Delete record?');
}
</SCRIPT>-->
   </center></td>
  </tr>
 </table>

<jsp:include page="Footer.jsp" flush="true"/>
</body>
</html>
<%%>
<%
if ( stat != null ) stat.close();
if ( conn != null ) conn.close();
%>
<%!


  String MatchesAction(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpSession session, javax.servlet.jsp.JspWriter out, String sAction, String sForm, java.sql.Connection conn, java.sql.Statement stat) throws java.io.IOException {
  
    String sMatchesErr ="";
    try {

      if (sAction.equals("")) return "";
      String sSQL="";
      String transitParams = "";
      String primaryKeyParams = "";
      String sQueryString = "";
      String sPage = "";
      String sParams = "";
      String sActionFileName = "AdminBetResults.jsp";
      String sWhere = " ";
      boolean bErr = false;
      long iCount = 0;

      String pPKmatch_id = "";
      
      if (sAction.equalsIgnoreCase("cancel") ) {
        try {
          if ( stat != null ) stat.close();
          if ( conn != null ) conn.close();
        }
        catch ( java.sql.SQLException ignore ) {}
        response.sendRedirect (sActionFileName);
        return "sendRedirect";
      }

      final int iinsertAction = 1;
      final int iupdateAction = 2;
      final int ideleteAction = 3;
      int iAction = 0;

      if ( sAction.equalsIgnoreCase("insert") ) { iAction = iinsertAction; }
      if ( sAction.equalsIgnoreCase("update") ) { iAction = iupdateAction; }

      // Create WHERE statement

      if ( iAction == iupdateAction || iAction == ideleteAction ) { 
        pPKmatch_id = getParam(request, "match_id");
        if (isEmpty(pPKmatch_id)) return sMatchesErr;       
        sWhere = "match_id=" + toSQL(pPKmatch_id, adNumber);
      }

      String fldmatch_id="";
      String fldhome_score="";
      String fldtimeout="";
      String flddesc="";

      // Load all form fields into variables
    
      fldmatch_id = getParam(request, "match_id");
      fldhome_score = getParam(request, "home_score");
	  flddesc = getParam(request, "desc");
	  fldtimeout = getParam(request, "timeout");
            
      // Validate fields
      if ( iAction == iinsertAction || iAction == iupdateAction ) {
        if ( isEmpty(fldhome_score) ) {
          sMatchesErr = sMatchesErr + "Eingabe im Feld \"Ergebnis\" wird benoetigt.<br>";
        }
        
        if (sMatchesErr.length() > 0 ) {
          return (sMatchesErr);
        }
      }


      sSQL = "";
      // Create SQL statement

      switch (iAction) {
        case iinsertAction :
            /*
            sSQL = "insert into matches (match_id, member_id, home_score, guest_score)" +
                   " values (" + 
                	toSQL(fldmatch_id, adNumber) + "," +
                        toSQL(fldmember_id, adNumber) + "," +
                        toSQL(fldhome_score, adNumber) + "," +
                        toSQL(fldguest_score, adNumber) + ")";
            */
            break;
  
      	case iupdateAction:
            sSQL = "update matches set " +
                "home=" + toSQL(fldhome_score, adNumber);
            sSQL = sSQL + " where " + sWhere;
            break;
      
      	case ideleteAction:
           sSQL = "delete from Matches where " + sWhere;
           break;
      }

      if ( sMatchesErr.length() > 0 ) return sMatchesErr;
      try {
        if ( iAction == iupdateAction) {
        	MatchResultUpdator u = new MatchResultUpdator (conn);
        	u.updateBetResult(toSQL(pPKmatch_id, adNumber), toSQL(fldhome_score, adNumber));
        } else {
        	// Execute SQL statement
        	stat.executeUpdate(sSQL);
        }
      }
      catch(java.sql.SQLException e) {
        sMatchesErr = e.toString(); return (sMatchesErr);
      }
  
      try {
        if ( stat != null ) stat.close();
        if ( conn != null ) conn.close();
      }
      catch ( java.sql.SQLException ignore ) {}
      response.sendRedirect (sActionFileName);

      return "sendRedirect";
    }
    catch (Exception e) {out.println(e.toString()); }
    return (sMatchesErr);
  }


  void Matches_Show(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpSession session, javax.servlet.jsp.JspWriter out, String sMatchesErr, String sForm, String sAction, java.sql.Connection conn, java.sql.Statement stat) throws java.io.IOException {
    try {

      String sSQL="";
      String sQueryString = "";
      String sPage = "";
      String sWhere = "";
      String transitParams = "";
      String transitParamsHidden = "";
      String requiredParams = "";
      String primaryKeyParams ="";
      java.util.Hashtable rsHash = new java.util.Hashtable();
      java.sql.Statement tmpStat = null;
      
      String pmatch_id = "";

      String fldmatch_id="";
      String fldhome_score="";
      String fldtimeout="";
      String flddesc="";

      boolean bPK = true;

      if ( "".equals(sMatchesErr)) {
        // Load primary key and form parameters        
        fldmatch_id = getParam( request, "match_id");
        fldhome_score = getParam(request, "home_score");
        pmatch_id = getParam( request, "match_id");
      }
      else {
        // Load primary key, form parameters and form fields
        fldmatch_id = getParam( request, "match_id");
        fldhome_score = getParam(request, "home_score");
        fldtimeout = getParam(request, "timeout");
		flddesc = getParam(request, "desc");
        pmatch_id = getParam( request, "match_id");
      }
	   
      if ( isEmpty(pmatch_id)) { bPK = false; }      
      
      sWhere += "match_id=" + toSQL(pmatch_id, adNumber);    
	  sSQL = "select * from matches where " + sWhere;

      java.sql.ResultSet rs = null;

      if ( bPK &&  ! (sAction.equals("insert") && "Matches".equals(sForm))) {
        
        // Open recordset
        rs = openrs( stat, sSQL);
        rs.next();
        String[] aFields = getFieldsName( rs );
              
        getRecordToHash( rs, rsHash, aFields );
        rs.close();
        
        if ( "".equals(sMatchesErr)) {                
          fldmatch_id = (String) rsHash.get("match_id");
		  flddesc = (String) rsHash.get("description");
          fldhome_score = (String) dLookUp(tmpStat, "matches", "home_score", "match_id=" + fldmatch_id);
          fldtimeout = (String) dLookUp(tmpStat, "matches", "timeout", "match_id=" + fldmatch_id);          
        }

        if (sAction.equals("") || ! "Matches".equals(sForm)) {
          fldmatch_id = (String) rsHash.get("match_id");         
		  flddesc = (String) rsHash.get("description");       
          fldhome_score = (String) dLookUp(tmpStat, "matches", "home_score", "match_id=" + fldmatch_id);
          fldtimeout = (String) dLookUp(tmpStat, "matches", "timeout", "match_id=" + fldmatch_id);          
        }
      }
      else {
        
        if ( "".equals(sMatchesErr)) {
          fldmatch_id = toHTML(getParam(request,"match_id"));
          flddesc = (String) rsHash.get("description");       
          fldhome_score = (String) dLookUp(tmpStat, "matches", "home_score", "match_id=" + fldmatch_id);
          fldtimeout = (String) dLookUp(tmpStat, "matches", "timeout", "match_id=" + fldmatch_id);          
        }
      }

      out.println("<center>");
      out.println("&nbsp;<br><br>");     
      out.println("<table bgcolor=\"#000000\" cellspacing=\"2\" cellpadding=\"3\">");
      out.println("<tr>\n"+
      		"<td style=\"background-color: #FFCC66\" colspan=\"6\"><center><font style=\"font-size: 14pt; font-weight: bold\">Ergebnis f&uuml;r Spiel Nummer " + toHTML((String) getParam(request, "match_id")) + "</font></center></td>\n</tr>");
      if ( ! sMatchesErr.equals("")) {
        out.println("<tr>\n<td style=\"background-color: #006600\" colspan=\"6\"><font style=\"font-size: 10pt; color: #ffffff\">"+sMatchesErr+"</font></td>\n</tr>");
      }
      sMatchesErr="";
      out.println("<form method=\"get\" action=\""+sFileName+"\" name=\"Matches\">");

      // Show form field
      out.print("<tr>\n");
      out.print("<td style=\"background-color: #FFCC66\"><font style=\"font-size: 12pt; font-weight: bold\">Tippschluss</font></td>");
      out.print("<td style=\"background-color: #FFCC66\"><font style=\"font-size: 12pt; font-weight: bold\">Wette</font></td>");
      out.print("<td style=\"background-color: #FFCC66\"><font style=\"font-size: 12pt; font-weight: bold\">Ergebnis</font></td>");
      out.println("\n</tr>");
            
      out.print("<tr>\n");
      out.print("<td style=\"background-color: #ffffff\"><font style=\"font-size: 12pt; font-weight: bold\">" + toHTML(fldtimeout) + "</font></td>");
      out.print("<td style=\"background-color: #ffffff\"><font style=\"font-size: 12pt; font-weight: bold\">" + toHTML(flddesc) + "</font></td>");
      out.print("<td style=\"background-color: #006600\">" + getTeamList(fldhome_score, conn, stat) + "</td>");
      out.println("\n</tr>");
      out.print("<tr>\n<td bgcolor=\"ffffff\" colspan=\"6\" align=\"right\">");

      if ( bPK && ! (sAction.equals("insert") && "Matches".equals(sForm))) {
        out.print("<input type=\"submit\" value=\"Update\" onclick=\"document.Matches.FormAction.value = 'update';\"> ");        
        out.print("<input type=\"submit\" value=\"Cancel\" onclick=\"document.Matches.FormAction.value = 'cancel';\">");
        //out.print("<input type=\"submit\" value=\"Delete\" onclick=\"document.Matches.FormAction.value = 'delete';\">");
        out.print("<input type=\"hidden\" name=\"FormName\" value=\"Matches\">");
        out.print("<input type=\"hidden\" value=\"update\" name=\"FormAction\">");
      }
      
      else {
        out.print("<input type=\"submit\" value=\"Update\" onclick=\"document.Matches.FormAction.value = 'update';\">");
        out.print("<input type=\"submit\" value=\"Cancel\" onclick=\"document.Matches.FormAction.value = 'cancel';\">");
        out.print("<input type=\"hidden\" name=\"FormName\" value=\"Matches\">");
        out.print("<input type=\"hidden\" value=\"update\" name=\"FormAction\">");
      }
      
      out.print("<input type=\"hidden\" name=\"timeout\" value=\""+toHTML(fldtimeout)+"\">");
      out.print("<input type=\"hidden\" name=\"match_id\" value=\""+toHTML(fldmatch_id)+"\">");
      out.print("<input type=\"hidden\" name=\"desc\" value=\""+toHTML(flddesc)+"\">");
      out.print(transitParamsHidden+requiredParams+primaryKeyParams);
      out.println("</td>\n</tr>\n</form>\n</table></center>&nbsp;<br><br>");

    }
    catch (Exception e) { out.println(e.toString()); }
  }
  
	private String getTeamList(String actualresult, java.sql.Connection conn, java.sql.Statement stat) {

		StringBuffer sb = new StringBuffer();
		
		// Execute SQL statement
		String teamsql = "SELECT team_id as teamid, name as teamname FROM teams ORDER BY name";

  		try {
			String selected = "";
			int i_actualresult = -1;

			try {
				i_actualresult = Integer.parseInt(actualresult);
			} catch (Exception e) {
			}
			
        	// Execute SQL statement
        	java.sql.ResultSet rs = stat.executeQuery(teamsql);
			
			// Build list
			sb.append("<select name=\"home_score\">");

			while (rs.next()) {
				if (rs.getInt("teamid") == i_actualresult) selected = "selected"; else selected = "";
				sb.append("<option " + selected + " value=\"" + rs.getInt("teamid") + "\">" + rs.getString("teamname") + "</option>");
			}		

			sb.append("</select>");

      	} catch(java.sql.SQLException e) {
        	String teamsqlerr = e.toString();
        	return (teamsqlerr);
      	}
		
		return sb.toString();
	}
%>